package com.bbcare.followup.plat.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bbcare.comm.*;
import com.bbcare.department.plat.service.impl.DepartmentBasicService;
import com.bbcare.followup.plat.dto.request.MessageNotifyRecordReqDTO;
import com.bbcare.followup.plat.service.IMessageNotifyRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;


/**
 * 消息通知记录 控制器
 *
 * @author liqianlong
 * @since 2021-04-07
 */
@RestController
@RequestMapping("/message/notify/record")
public class MessageNotifyRecordController {
    @Autowired
    private IMessageNotifyRecordService messageNotifyRecordService;

    @Autowired
    private DepartmentBasicService departmentBasicService;

    /**
     * 标记消息分页
     * @param reqDTO 请求参数
     * @return
     */
    @PostMapping("/listPage")
    public ResultModel listPage(@RequestBody MessageNotifyRecordReqDTO reqDTO,HttpServletRequest request) {
        Map<String,Object> map = messageNotifyRecordService.listPage(reqDTO,request);
        return new ResultModel(map);
    }

    /**
     * 标记消息已读
     * @param id 消息ID
     * @return
     */
    @PostMapping("markRead")
    public ResultModel markMessageNotifyRead(@RequestParam("id") String id) {
        messageNotifyRecordService.markMessageNotifyRead(id);
        return new ResultModel("标记已读");
    }

    /**
     * 发送通用消息
     * @param arg
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("/sendMessage")
    public ResultModel sendMessage(@RequestBody String arg,HttpServletRequest request) throws Exception {
        JSONObject jsonArg = null;
        try {
            jsonArg = JSON.parseObject(arg);
        } catch (Exception ex) {
            ex.printStackTrace();
            // 后续重写异常类
            throw (new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_FORMAT_ERROR, "入参节点错误，非json格式！"));
        }
        JSONObject jsonData = jsonArg.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD);
        if (null == jsonData) {
            // 前台必须传data节点，否则报错返回（后续重写异常类）
            throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！");
        }

        // 从session中获取，这里先写死
        Map<String, Object> session = ToolUtils.getSessionInfo(request);
        String authorId = (String) session.get("authorId");
        String tenantId = (String) session.get("tenantId");
        String authorName = (String) session.get("authorName");
        String deptId = (String) session.get("deptId");

        String platType = departmentBasicService.getDepartmentBasicDetails(deptId).getPlatType();
        String userId = jsonData.getString("userId");
        String userName = jsonData.getString("userName");
        String content = jsonData.getString("content");

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("userId",userId);
        jsonObject.put("userName", userName);
        jsonObject.put("content",content);
        jsonObject.put("platType",Integer.parseInt(platType));
        jsonObject.put("tenantId",tenantId);
        jsonObject.put("type",22);

        messageNotifyRecordService.sendMessage(jsonObject);
        return new ResultModel("发送成功");
    }
}
